<html><!-- Created using the cpp_pretty_printer from the dlib C++ library.  See http://dlib.net for updates. --><head><title>dlib C++ Library - example_mex_struct.cpp</title></head><body bgcolor='white'><pre>
<font color='#009900'>// The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt
</font>
<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='call_matlab.h.html'>call_matlab.h</a>"
<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='dlib/matrix.h.html'>dlib/matrix.h</a>"
<font color='#0000FF'>using</font> <font color='#0000FF'>namespace</font> dlib;
<font color='#0000FF'>using</font> <font color='#0000FF'>namespace</font> std;


<font color='#009900'>/*
    This mex function takes a MATLAB struct, prints a few of its fields,
    and then returns a new struct.

    For example, you can call this function in MATLAB like so:
        input = {}
        input.val = 2
        input.stuff = 'some string'
        output = example_mex_struct(input)

        output.number
        output.number2
        output.sub.stuff
        output.sub.some_matrix
*/</font>


<font color='#0000FF'><u>void</u></font> <b><a name='mex_function'></a>mex_function</b> <font face='Lucida Console'>(</font>
    <font color='#0000FF'>const</font> matlab_struct<font color='#5555FF'>&amp;</font> input,
    matlab_struct<font color='#5555FF'>&amp;</font> output 
<font face='Lucida Console'>)</font> 
<b>{</b>
    <font color='#0000FF'><u>int</u></font> val <font color='#5555FF'>=</font> input["<font color='#CC0000'>val</font>"];
    string stuff <font color='#5555FF'>=</font> input["<font color='#CC0000'>stuff</font>"];

    <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>input.<font color='#BB00BB'>has_field</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>val2</font>"<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font> 
    <b>{</b>
        string val2 <font color='#5555FF'>=</font> input["<font color='#CC0000'>val2</font>"];
        cout <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>The optional val2 field was set to: </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> val2 <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> endl;
    <b>}</b>

    cout <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>val: </font>"<font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> val <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> endl;
    cout <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>stuff: </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> stuff <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> endl;

    output["<font color='#CC0000'>number</font>"] <font color='#5555FF'>=</font> <font color='#979000'>999</font>;

    output["<font color='#CC0000'>number2</font>"] <font color='#5555FF'>=</font> <font color='#979000'>1000</font>;
    output["<font color='#CC0000'>sub</font>"]["<font color='#CC0000'>stuff</font>"] <font color='#5555FF'>=</font> "<font color='#CC0000'>some other string</font>";
    matrix<font color='#5555FF'>&lt;</font><font color='#0000FF'><u>double</u></font><font color='#5555FF'>&gt;</font> m <font color='#5555FF'>=</font> <font color='#BB00BB'>randm</font><font face='Lucida Console'>(</font><font color='#979000'>2</font>,<font color='#979000'>2</font><font face='Lucida Console'>)</font>;
    output["<font color='#CC0000'>sub</font>"]["<font color='#CC0000'>some_matrix</font>"] <font color='#5555FF'>=</font> m;
<b>}</b>



<font color='#009900'>// #including this brings in all the mex boiler plate needed by MATLAB.
</font><font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='mex_wrapper.cpp.html'>mex_wrapper.cpp</a>"


</pre></body></html>